import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author LKQ
 * @date 2022/1/12 16:16
 * @description 以右端点排序
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] intervals = {{1,2}, {2, 3}, {3, 4}, {1,3 }};
        solution.eraseOverlapIntervals(intervals);
    }
    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length == 0) {
            return 0;
        }
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] o1, int[] o2) {
                return o1[1] - o2[1];
            }
        });
        int n = intervals.length;
        int right = intervals[0][1];
        int ans = 1;
        for (int i = 1; i < n; i++) {
            if (intervals[i][0] >= right) {
                ++ans;
                right = intervals[i][1];
            }
        }
        return n - ans;
    }
}
